﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DatabaseManagerLib;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace ServerLib
{
    public sealed class RemoteServer : MarshalByRefObject, IRemoteServer
    {
        private DatabaseManager _managerDb; // object for used server's database
        private ArrayList _clients;
        private String _chatSession;

        #region Constructor
        public RemoteServer()
        {
            _managerDb = new DatabaseManager(Properties.Resources.ConnectionDbSource, Properties.Resources.ConnectionDbNameDatabase);
            _chatSession = "";
            _clients = new ArrayList();
            Console.WriteLine(_managerDb.Connect());
        }

        #endregion

        public ArrayList GetListOfClients()
        {
            return _clients;
        }

        public ClientInfo GetClientInfo(String login)
        {
            return new ClientInfo(login,
                _managerDb.GetName(login),
                _managerDb.GetLastName(login),
                _managerDb.GetBirthDay(login),
                _managerDb.GetSex(login) == "Male" ? ClientInfo.EnumSex.Male : ClientInfo.EnumSex.Female
                );
        }

        public String GetChatSession()
        {
            return _chatSession;
        }

        public void AddMessage(String login, String message)
        {
            if (login != null && message != null)
            {
                lock(_chatSession)
                {
                    _chatSession += login + " : " + message + Environment.NewLine;
                }
            }
        }

        public void AddClient(String login)
        {
            if (login != null)
            {
                lock (_clients)
                {
                    _clients.Add(login);
                }
            }
        }
        public void RemoveClient(String login)
        {
            if (login != null)
            {
                lock (_clients)
                {
                    _clients.Remove(login);
                }
            }
        }
        public bool CheckPassword(String login, String password)
        {
            String truePassword = _managerDb.GetPassword(login);
            if (password == truePassword)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }

        public bool CheckLogin(String login)
        {
            List<String> list = _managerDb.GetListLogin();
            foreach (String clientLogin in list)
            {
                if (clientLogin == login)
                {
                    return false;
                }
            }
            return true;
        }

        public void CreateUser(String login, String password)
        {
            _managerDb.CreateUser(login, password);
        }

        public void RemoteUser(String login, String password)
        {
            if (CheckPassword(login, password))
            _managerDb.DeleteUser(login);
        }

        public void SetUserInfo(String login, String password, String name, String lastName, String birthday, ClientInfo.EnumSex sex)
        {
            if (CheckPassword(login, password))
            {
                if (name.TrimEnd() != "")
                {
                    _managerDb.SetName(login, name);
                }
                if (lastName.TrimEnd() != "")
                {
                    _managerDb.SetLastName(login, lastName);
                }
                if (birthday.TrimEnd() != "")
                {
                    _managerDb.SetBirthDay(login, birthday);
                }
                _managerDb.SetSex(login, sex.ToString());
            }
        }

        public void SetUserInfo(String login, String name, String lastName, String birthday, ClientInfo.EnumSex sex)
        {
            if (name.TrimEnd() != "")
            {
                _managerDb.SetName(login, name);
            }
            if (lastName.TrimEnd() != "")
            {
                _managerDb.SetLastName(login, lastName);
            }
            if (birthday.TrimEnd() != "")
            {
                _managerDb.SetBirthDay(login, birthday);
            }
            _managerDb.SetSex(login, sex.ToString());

        }

        public void ChangePassword(String login, String password)
        {
            _managerDb.SetPassword(login, password);
        }
    }
}
